<!--
 * @Author: malgee
 * @Date: 2025-04-23 13:49:53
 * @LastEditors: maling
 * @LastEditTime: 2025-04-27 13:18:58
 * @FilePath: /vue3-ts-cms/src/components/nav-menu/src/nav-menu.vue
 * @Description: 
 * 
-->
<template>
  <div class="nav-menu">
    <div class="logo">
      <img class="logo-img" src="@/assets/logo.png" alt="logo">
      <span class="logo-text" v-if="!collapse">Vue3+TS</span>
    </div>
    <el-menu 
      :collapse="collapse"
      mode="vertical"
      class="el-menu-vertical"
    >
      <template v-for="item in userMenus" :key="item.id">

        <!-- 1. 一级菜单 -->
        <template v-if="item.type === 1">
          <el-sub-menu :index="item.id.toString()">
            <template #title>
              <el-icon v-if="item.icon" color="#fff">
                <Setting v-if="item.icon === 'Setting'"/>
                <Menu v-if="item.icon === 'Menu'"/>
                <Memo v-if="item.icon === 'Memo'"/>
              </el-icon>
              <span>{{ item.name }}</span>
            </template>
            <!-- 1.1. 二级菜单 type === 2 -->
            <template v-for="child in item.children" :key="child.id">
              <el-menu-item :index="child.id.toString()" @click="handleMenuItemClick(child)">
                <span>{{ child.name}}</span>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>

        <!-- 2. 二级菜单 -->
        <template v-else-if="item.type === 2">
          <el-menu-item :index="item.id.toString()" @click="handleMenuItemClick(item)">
            <span>{{ item.name }}</span>
          </el-menu-item>
        </template>
        
      </template>

    </el-menu>
  </div>
</template>

<script setup lang="ts">
import { Setting, Menu, Memo } from '@element-plus/icons-vue'
import { useStore } from '@/store';
import { computed } from 'vue';
import { defineProps } from 'vue';
import { useRouter, useRoute } from 'vue-router'

const props = defineProps({
  collapse: {
    type: Boolean,
    default: false
  }
})


// store
const store = useStore()
const userMenus = computed(() => store.state.login.userMenus)


// router
const router = useRouter()
const route = useRoute()
const currentPath = route.path  // 获取当前路径, 

const handleMenuItemClick = (item: any) => {  
  router.push({
    path: item.url ?? '/not-found'
  })
}

</script>

<style scoped lang="less">
.nav-menu {
 height: 100%;
 background-color: #001529; 
 .logo {
    display: flex;
    height: 28px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;

    .logo-img {
      height: 100%;
      margin: 0 10px;
    }

    .logo-text {
      font-size: 16px;
      font-weight: 700;
      color: white;
    }
 }

 ::v-deep .el-menu {
  border-right: none;
  background-color: #001529 !important;
 }
  ::v-deep .el-submenu__title {
    color: #fff !important;
    background-color: #001529 !important;
  }

  ::v-deep .el-sub-menu__title {
    color: #fff !important;
    &:hover {
      color: #fff !important;
      background-color: #0a61bd6f !important;
    }
  }

  .el-menu-item {
    color: #fff !important; // 菜单
  }
    // hover 高亮
  .el-menu-item:hover {
    color: #fff !important; // 菜单
    background-color: #0a61bd6f !important;
  }

  .el-menu-item.is-active {
    color: #fff !important;
    background-color: #0a60bd !important;
  }
}
.el-menu-vertical:not(.el-menu--collapse) {
  width: 100%;
  height: calc(100% - 48px);
}
</style>